Plotting Multiple Curves

In this lesson, we will learn some different ways of plotting multiple graphs and how to label them.

In this course, we will be using the object-oriented approach when working with matplotlib.The main idea with object-oriented programming is to have objects that one can apply functions and actions to, and no object or program states should be global. The real advantage of the object-oriented approach in plotting becomes apparent when more than one figure is created, or when a figure contains more than one plot.

Multiple curves on the same graph#

We can draw multiple curves on the same graph by repeatedly using the plot command.

If you don’t specify a color for a plotting statement, matplotlib will use its default colors. The first three default colors are special shades of blue, orange and green.

You can find more colors using the command `help(“matplotlib.pyplot.plot”) below:

Adding legends#

When plotting multiple curves on the same graph, it is imperative to add a legend to make the graph more readable. There are two ways to add legends:

  1. Use the legend() method of the axis object and pass a list of legend texts for the curves plotted previously.
plt.plot(x1, y1)
plt.plot(x2, y2)
plt.legend(["curve 1", "curve 2"]) 
  1. Use the label = "curve 1" keyword argument when plots are added to the figure, and then use the legend() method without arguments to add the legend to the figure.
plt.plot(x1, y1, label="curve 1")
plt.plot(x2, y2, label="curve 2")
legend()

While method 1 makes the code look cleaner, method 2 removes the hassle of separately changing the legend when making changes to the graph. It is entirely up to the user which method they choose.

The legend function takes an optional keyword argument loc that can be used to specify where in the figure the legend is to be drawn.

plt.legend(loc=0) # let matplotlib decide the optimal location 
plt.legend(loc=1) # upper right corner
plt.legend(loc=2) # upper left corner
plt.legend(loc=3) # lower left corner
plt.legend(loc=4) # lower right corner 

The example below uses method 1 for implementing legends.

Making a new figure#

Whenever there is a plot statement, a figure with a default size is automatically created, and all subsequent plotting statements in the code are added to the same figure. If you want the figure to be a different size, you can create a figure of the desired size first using the plt.figure(figsize=(width, height)) syntax.

Any subsequent plotting statement in the code is then added to the figure. You can even create a second figure or third or fourth and so on.

Subplots#

We can use many axis layout managers in matplotlib if we do not care about the positioning of the axis in the figure window. The most commonly used is subplot:

Using the subplots, we can plot more than one graph on the same figure by dividing the figure into a grid. We need to specify the number of rows and columns in the subplots() function.

plt.subplots(rows, columns)
Created with Fabric.js 1.6.0-rc.1 Plot numbers for a 2x2 grid 0,0 1,0 0,1 1,1

This plt.subplots(rows, columns) returns an ndarray with subplot objects in it. Each subplot can be accessed using its corresponding index. We can also define the value for the figsize property as one of the arguments of the subplot() command. Let’s look at an example of this below:

You might notice the fig.tight_layout() command in line 22.

fig.tight_layout() automatically adjusts the position of the axes on the figure so that there is no overlapping content.

Remove this line and see what happens.

Alternate method#

We can also use the add_subplot() function to add subplots to a figure. This alternate method gives us the flexibility to specify the type of axes for each subplot. We will see examples of this in the next couple of lessons. For now, let’s understand the syntax:

fig.add_subplot(nrows, ncols, index)
  • nrows is the number of rows
  • ncols is the number of columns
  • index is the index of the subplot in the grid.
fig.add_subplot(1, 2, 1)

can also be written as the following for simplicity purposes:

fig.add_subplot(121)

Let’s see an implementation of this below:


In the next lesson, we will learn about setting up the axes.

Important Note!

Setting Up the Axes